<template>
  <div>
    <el-dialog
      title="发布任务"
      :visible.sync="editVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="任务模板：">
          <div>
            {{ item.name }}
          </div>
          <!-- <el-input v-model="item.name" placeholder="请输入模板名称" clearable /> -->
        </el-form-item>
        <el-form-item label="任务频次：">
          <div>{{ item.frequency == 'monthly' ? '按月' : '一次性' }}</div>
        </el-form-item>
        <el-form-item label="任务名称：">
          <div>{{ item.name + '-' + (item.frequency == 'monthly' ? '按月' : '一次性') }}</div>
          <!-- <el-input v-model="form.name" placeholder="请输入任务名称" clearable /> -->
        </el-form-item>
        <el-form-item label="开始时间：">
          <el-date-picker
            v-model="form.start_date"
            style="width: 100%;"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="结束时间：">
          <el-date-picker
            v-model="form.end_date"
            style="width: 100%;"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="填报单位：">
          <el-select v-model="district_ids" multiple style="width: 100%" clearable filterable placeholder="请选择填报单位">
            <el-option v-for="dist in districtList" :key="dist.id" :label="dist.name" :value="dist.id" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="任务提醒：">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button v-preventReClick="3000" type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { taskPublish } from '@/api/table'
// import { getDistrictList } from '@/api/authority'
export default {
  props: {
    editVisible: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {}
    },
    districtList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        end_date: '',
        start_date: ''
      },
      district_ids: [],
      fileList: []
    }
  },
  created() {
    // this.getDistirct()
    this.form = { ...this.item }
    this.form.districts.forEach(ele => {
      this.district_ids.push(ele.id)
    })
  },
  methods: {
    onSubmit() {
      const data = {
        id: this.item.id,
        start_date: this.form.start_date,
        end_date: this.form.end_date,
        district_ids: this.district_ids
      }
      taskPublish(data).then(res => {
        if (res.status == 200) {
          this.$message({
            message: '发布成功',
            type: 'success'
          })
          this.$emit('update', true)
        }
      })
    },
    handleClose() {
      this.$emit('update', false)
    }
  }
}
</script>
